package com.dgut.admodule.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.admodule.entity.Adsbid;

public class AdsbidQueryDTO {
	private Long id;
	private String adsName;
	private String adsDecription;
	private String cName;
	private Double bidPrice;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getAdsName() {
		return adsName;
	}
	public void setAdsName(String adsName) {
		this.adsName = adsName;
	}
	public String getAdsDecription() {
		return adsDecription;
	}
	public void setAdsDecription(String adsDecription) {
		this.adsDecription = adsDecription;
	}
	public String getcName() {
		return cName;
	}
	public void setcName(String cName) {
		this.cName = cName;
	}
	public Double getBidPrice() {
		return bidPrice;
	}
	public void setBidPrice(Double bidPrice) {
		this.bidPrice = bidPrice;
	}
	
	public static Specification<Adsbid> getSpecification(AdsbidQueryDTO adsbidQueryDTO)
	{
		Specification<Adsbid> spec = new Specification<Adsbid>() {
			public Predicate toPredicate(Root<Adsbid> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != adsbidQueryDTO && !StringUtils.isEmpty(adsbidQueryDTO.getId())) {
					 Predicate  p1 =  cb.equal(root.get("id"),adsbidQueryDTO.getId());
					 list.add(p1);
				 }
				 if(null != adsbidQueryDTO && !StringUtils.isEmpty(adsbidQueryDTO.getcName())) {
					 Predicate  p2 =  cb.like(root.get("cName").as(String.class),"%"+ adsbidQueryDTO.getcName()+"%");
					 list.add(p2);
				 }
				 if(null != adsbidQueryDTO && !StringUtils.isEmpty(adsbidQueryDTO.getBidPrice())) {
					 Predicate  p3 =  cb.greaterThanOrEqualTo(root.get("bidPrice"), adsbidQueryDTO.getBidPrice());
					 list.add(p3);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
